package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.anno.Log;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
@Service
public class ClazzServiceimpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;


    @Override
    public PageBean page(ClazzQueryParam clazzQueryParam) {
        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());
        List<Clazz> rows = clazzMapper.selectPage(clazzQueryParam);
        for (Clazz clazz : rows)
            if ( clazz.getEndDate().isAfter(LocalDate.now())&& clazz.getBeginDate().isBefore(LocalDate.now())){
                clazz.setStatus("在读");
            }else if( clazz.getBeginDate().isAfter(LocalDate.now())){
                clazz.setStatus("未开课");
            }else {
                clazz.setStatus("已结课");
            }

        Page<Clazz> p = (Page<Clazz>) rows;
        return  new PageBean(p.getTotal(),p.getResult());
    }
    @Log
    @Override
    public void add(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.add(clazz);
    }
    @Log
    @Override
    public void delete(Integer id) {
        clazzMapper.delete(id);
    }



    @Override
    public Clazz findById(Integer id) {
        return clazzMapper.findById(id);
    }
    @Log
    @Override
    public void update(Clazz clazz) {
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public List<Clazz> findAll() {
        return clazzMapper.findAll();
    }

//    @Override
//    public List<Clazz> selectPage() {
//        return clazzMapper.findAll();
//    }


}
